package com.tistory.wonsama.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.PooledConnection;

import oracle.jdbc.pool.OracleConnectionPoolDataSource;

import com.tistory.wonsama.util.WProperties;

/**
 * CONNECTION POOL FOR ORACLE
 * @author wonsama
 * @since 2011.12.28
 */
public class WOracleConnectionPool{

	private static WOracleConnectionPool _instance;
	private OracleConnectionPoolDataSource ocpds;
	
	private final String JDBC_PROPERTIES = "resource/jdbc.properties";
	private PooledConnection pc;
	
	/**
	 * 생성자
	 * @since 2011.12.28
	 */
	private WOracleConnectionPool()
	{
		try{
			ocpds = new OracleConnectionPoolDataSource();
			
			Properties p = WProperties.loadProperties( JDBC_PROPERTIES );
			if( p != null ){
				ocpds.setURL( 		p.getProperty("url")	);
				ocpds.setUser( 		p.getProperty("user")	);
				ocpds.setPassword( 	p.getProperty("password")	);
			
				pc = ocpds.getPooledConnection();
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 인스턴스 객체를 얻어온다
	 * @return 인스턴스
	 * @since 2011.12.28
	 */
	public static WOracleConnectionPool getInstance()
	{
		if(_instance == null)
			_instance = new WOracleConnectionPool();
		
		return _instance;
	}
	
	/**
	 * GET CONNECTION FROM POOL
	 * IF USE COMPLETE , PLZ CLOSE CONNECTION
	 * @return 커넥션
	 * @since 2011.12.28
	 */
	public Connection getConnection()
	{
		Connection conn = null;

		try {
			conn = pc.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return conn;
	}

	/**
	 * CLOSE POOL
	 * @since 2011.12.28
	 */
	public void closePool()
	{
		try{
			pc.close();
			ocpds.close();	
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
